package com.java2001.swagger.config;


import com.java2001.swagger.controller.HelloController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("A");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("B");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("C");
    }




    //配置了Swagger的bean实例
    @Bean
    public Docket docket(Environment environment) {


        Profiles profiles = Profiles.of("dev","test");

        //判断是否处在dev、test环境
        boolean flag = environment.acceptsProfiles(profiles);

        System.out.println("flag ========================>" + flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) //配置swagger信息
                .groupName("Ta1entO_o")
                .enable(flag)  //是否启动swagger-ui
                .select()
                //RequestHandlerSelectors:配置要扫描接口的方式
                .apis(RequestHandlerSelectors.basePackage("com.java2001.swagger.controller"))    //指定要扫描的包
                // .apis(RequestHandlerSelectors.any())    //扫描全部
                // .apis(RequestHandlerSelectors.none())    //不扫描
                // .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))    //扫描方法上的注解
                // .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))    //扫描类上的注解

                //过滤什么请求
                //此过滤的意思是只有带有   /java2001/**这样的请求地址才能被扫描到
                // .paths(PathSelectors.ant("/java2001/**"))
                .build();

    }

    private ApiInfo apiInfo() {

        //作者信息
        Contact contact = new Contact("Ta1entO_o", "http://baidu.com", "232281446@qq.com");

        return new ApiInfo("Ta1entO_o 的 SwaggerAPI文档",
                "SwaggerAPI文档描述",
                "v1.0",
                "http://baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }


}
